//
//  TGMetaDataTool.m
//  团购
//
//  Created by apple on 13-11-9.
//  Copyright (c) 2013年 itcast. All rights reserved.
//

#import "MZMetaDataTool.h"
#import "MZSection.h"
#import "NSObject+Value.h"
#import "MZNature.h"

#define kFilePath [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0] stringByAppendingPathComponent:@"visitedCityNames.data"]

@interface MZMetaDataTool()
{
    NSMutableArray *_visitedCityNames; // 存储曾经访问过城市的名称
    
    NSMutableArray *_totalCities; // 存放所有的城市 key 是城市名  value 是城市对象
    
    MZSection *_visitedSection;
    MZSection *_section;// 最近访问的城市组数组
    NSMutableDictionary *_allnature;
}
@end

@implementation MZMetaDataTool
singleton_implementation(MZMetaDataTool)

- (id)init
{
    if (self = [super init]) {
        // 初始化项目中的所有元数据
        
        // 存放所有的城市
        _totalCities = [NSMutableArray  array];
        _allnature = [NSMutableDictionary dictionary];
        // 存放所有的城市组
        NSMutableArray *tempSections = [NSMutableArray array];
        
        // 2.添加A-Z组
        // 加载plist数据
        NSArray *azArray  = @[@{@"name" : @"A", @"natures" : @[@"阿昌族"]},@{@"name" : @"B", @"natures" :  @[@"布依族",@"布朗族",@"保安族"]},
                            @{@"name" : @"C", @"natures" : @[@"朝鲜族"]},@{@"name" : @"D", @"natures" : @[@"傣族",@"德昂族",@"东乡族",@"独龙族",@"侗族",@"达斡尔族"]},
                            @{@"name" : @"E", @"natures" : @[@"鄂伦春族",@"鄂温克族",@"俄罗斯族"]},
                              //  @{@"index" : @"F", @"key" : @[]},
                            @{@"name" : @"G", @"natures" : @[@"仡佬族",@"高山族"]},@{@"name" : @"H", @"natures" : @[@"汉族",@"赫哲族",@"哈尼族",@"回族",@"哈萨克族"]},
                              //  @{@"index" : @"I", @"key" : @[]},
                            @{@"name" : @"J", @"natures" : @[@"京族",@"景颇族",@"基诺族"]},
                            @{@"name" : @"K", @"natures" : @[@"柯尔克孜族"]},@{@"name" : @"L", @"natures" : @[@"黎族",@"拉祜族",@"珞巴族",@"傈僳族"]},
                            @{@"name" : @"M", @"natures" : @[@"仫佬族",@"满族",@"蒙古族",@"毛南族",@"苗族",@"门巴族"]},@{@"name" : @"N", @"natures" : @[@"怒族",@"纳西族"]},
                              //  @{@"index" : @"O", @"key" : @[]},
                            @{@"name" : @"P", @"natures" : @[@"普米族"]},
                            @{@"name" : @"Q", @"natures" : @[@"羌族"]},
                              //  @{@"index" : @"R", @"key" : @[]},
                            @{@"name" : @"S", @"natures" : @[@"撒拉族",@"畲族",@"水族"]},@{@"name" : @"T", @"natures" : @[@"塔塔尔族",@"土族",@"土家族",@"塔吉克族"]},
                              //  @{@"name" : @"U", @"key" : @[]},@{@"index" : @"V", @"key" : @[]},
                            @{@"name" : @"W", @"natures" : @[@"塔吉克族",@"佤族",@"维吾尔族",@"乌兹别克族"]},@{@"name" : @"X", @"natures" : @[@"锡伯族"]},
                            @{@"name" : @"Y", @"natures" : @[@"裕固族",@"瑶族",@"彝族"]},@{@"name" : @"Z", @"natures" : @[@"藏族",@"壮族"]},
                              ];        for (NSDictionary *azDict in azArray) {
            // 创建城市组
            MZSection *section = [[MZSection alloc] init];
            [section setValues:azDict];
            [tempSections addObject:section];

//            // 遍历这组的所有城市
            for (NSString *city in section.natures) {
                
//                [_totalCities addObject:city];
                [_allnature setObject:city forKey:city];
//                NSLog(@"allnature = %@",_allnature);
            }
        }

        
        // 3.从沙盒中读取之前访问过的名称
        _visitedCityNames = [NSKeyedUnarchiver unarchiveObjectWithFile:kFilePath];
        if (_visitedCityNames == nil) {
            _visitedCityNames = [NSMutableArray array];
        }

////        // 4.添加最近访问组
        MZSection *visitedSection = [[MZSection alloc] init];
        visitedSection.name = @"最近访问";
        visitedSection.natures = [NSMutableArray array];
        [tempSections insertObject:visitedSection atIndex:0];
        _visitedSection = visitedSection;
//        [self setCurrentCity:visitedSection];
        for (NSString *name in _visitedCityNames) {
//            MZSection *city = _allnature[name];
            [visitedSection.natures addObject:name];
            NSLog(@"visit = %@",visitedSection.natures);
        }
        
        _totalCitySections = tempSections;
    }
    return self;
}

- (void)setCurrentCity:(NSString *)currentCity
{
    _currentCity = currentCity;
    
    // 1.移除之前的
    [_visitedCityNames removeObject:currentCity];
    
    // 2.将新的名放到最前面
    [_visitedCityNames insertObject:currentCity atIndex:0];
    
    // 3.将新的放到_visitedSection的最前面
    [_visitedSection.natures removeObject:currentCity];
    [_visitedSection.natures insertObject:currentCity atIndex:0];
    
    [NSKeyedArchiver archiveRootObject:_visitedCityNames toFile:kFilePath];
    
    // 发出通知
//    [[NSNotificationCenter defaultCenter] postNotificationName:@"change" object:nil];
}
@end
